﻿' ------------------------------------------------------------------------------
' This TraceListener subclass forwards write and flush activity to a LogControl.
' ------------------------------------------------------------------------------
' This file is part of the SQL Server 2008 Technology Demonstration. This
' application was designed and coded by Eric Weitzman, LandTime Inc, for
' Microsoft Corporation. Contact Ed Katibah at Microsoft about this demo.
' Copyright © 2008 Microsoft Corporation. All rights reserved.
' ------------------------------------------------------------------------------

Public Class LogListener
    Inherits TraceListener

    Dim m_control As LogControl
    Dim m_buffer As String

    Public Sub New(ByVal tdc As LogControl)
        m_control = tdc
    End Sub

    Public Overloads Overrides Sub Write(ByVal message As String)
        If m_buffer Is Nothing Then
            m_buffer = message
        Else
            m_buffer &= message
        End If
    End Sub

    Public Overloads Overrides Sub WriteLine(ByVal message As String)
        If m_buffer Is Nothing Then
            m_control.WriteLine(message)
        Else
            m_control.WriteLine(m_buffer & message)
            m_buffer = Nothing
        End If
    End Sub

    Public Overloads Sub Flush()
        If m_buffer Is Nothing Then Return
        m_control.WriteLine(m_buffer)
        m_control.Update()
        My.Application.DoEvents()
        m_buffer = Nothing
    End Sub

End Class
